﻿Imports System.Xml

Module searchFunctions

    ' Search types:
    ' - series (0)
    ' - season (1)
    ' - episode (2)

    Public Function searchTVDb(ByVal searchType As Integer, ByVal searchStr As String) As XmlNodeList
        Dim xml As New XmlDocument

        '-------------------------------------------------------------------
        'Series Search
        '-------------------------------------------------------------------

        If searchType = 0 Then
            xml.Load("http://www.thetvdb.com/api/GetSeries.php?seriesname=" & searchStr)
            searchTVDb = xml.GetElementsByTagName("Series")

            If searchTVDb.Count > 0 Then

                Dim series As XmlNode

                Dim provsid As String
                Dim seriesname As String
                Dim desc As String
                Dim language As String
                Dim airdate As String
                Dim banner As String

                For Each series In searchTVDb

                    provsid = ""
                    seriesname = ""
                    desc = ""
                    language = ""
                    airdate = "0000-00-00"
                    banner = ""

                    provsid = series.Item("id").InnerText
                    seriesname = series.Item("SeriesName").InnerText
                    Try
                        desc = series.Item("Overview").InnerText
                    Catch
                    End Try
                    language = series.Item("language").InnerText
                    Try
                        airdate = series.Item("FirstAired").InnerText
                    Catch
                    End Try
                    Try
                        banner = series.Item("banner").InnerText
                    Catch
                    End Try
                    updatetvDb("tvdb", provsid, seriesname, desc, language, airdate, banner)
                Next
            End If
        End If

        '-------------------------------------------------------------------
        'Season Search
        '-------------------------------------------------------------------



        '-------------------------------------------------------------------
        'Episode Search
        '-------------------------------------------------------------------
        If searchType = 2 Then

        End If

    End Function

    Public Function searchCache()

    End Function

    Public Function updatetvDb(ByVal provider As String, ByVal provsid As String, ByVal seriesname As String, ByVal desc As String, ByVal language As String, ByVal airdate As String, ByVal banner As String)
        Dim seriesID
        seriesID = makeMD5(provider & provsid)
        runSQL("INSERT IGNORE INTO localtvSeriesdb (`tvID`,`language`, `tvname`, `banner`, `airdate`, `desc`, `provider`, `provsid`) VALUES ('" & seriesID & "','" & language & "','" & cleanSQL(seriesname) & "','" & banner & "',DATE('" & airdate & "'),'" & cleanSQL(desc) & "','" & provider & "','" & provsid & "');")
    End Function


End Module
